<template>
	<view>
		<view v-if="salaryList.length === 0" class="no-salary">暂无合同信息</view>
		<view v-else-if="isCanHd == true" style="padding-bottom: 10rpx;">
			<view v-for="salary in salaryList" :key="salary.paymentMonth" class="salary-item"
				@click="goToDetails(salary.id)">
				<view class="date-info">
					<text class="label" :class="salary.status == '已终止' ? 'expired' : 'valid'">
						{{ salary.status }}
					</text>
					<text class="label">劳动合同</text>
				</view>
				<!-- 显示合同信息 -->
				<view class="salary-info">
					<view class="salary-detail">
						<text class="title">任职部门:</text>
						<text class="value">{{ salary.deptName }}</text>
					</view>
					<view class="salary-detail">
						<text class="title">担任岗位:</text>
						<text class="value">{{ salary.postName }}</text>
					</view>
					<view v-if="isShowSigin" class="salary-detail">
						<text class="title">签订日期:</text>
						<text class="value">{{ showSignDate(salary.signdate) }}</text>
					</view>
					<view class="salary-detail">
						<text class="title">合同有效期:</text>
						<text class="value">{{ salary.startdate }} 至 {{ salary.enddate }}</text>
					</view>
				</view>
				<!-- <view class="divider"></view> -->
				<!-- 分割线 -->
			</view>
		</view>
		<view v-else style="padding-bottom: 10rpx;" @touchmove.stop.prevent="() => {}">
			<view v-for="salary in salaryList" :key="salary.paymentMonth" class="salary-item"
				@click="goToDetails(salary.id)">
				<view class="date-info">
					<text class="label" :class="salary.status == '已终止' ? 'expired' : 'valid'">
						{{ salary.status }}
					</text>
					<text class="label">劳动合同</text>
				</view>
				<!-- 显示合同信息 -->
				<view class="salary-info">
					<view class="salary-detail">
						<text class="title">任职部门:</text>
						<text class="value">{{ salary.deptName }}</text>
					</view>
					<view class="salary-detail">
						<text class="title">担任岗位:</text>
						<text class="value">{{ salary.postName }}</text>
					</view>
					<view v-if="isShowSigin" class="salary-detail">
						<text class="title">签订日期:</text>
						<text class="value">{{ showSignDate(salary.signdate) }}</text>
					</view>
					<view class="salary-detail">
						<text class="title">合同有效期:</text>
						<text class="value">{{ salary.startdate }} 至 {{ salary.enddate }}</text>
					</view>
				</view>
				<!-- <view class="divider"></view> -->
				<!-- 分割线 -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		contractList
	} from "@/api/work/hrmContract";
	import {
		getDicts
	} from "@/api/system/dict/data.js";
import isHaveBug from "../../../utils/bug";

	export default {
		data() {
			return {
				isCanHd:true,
				isShowSigin:true,
				contractStatus: [],
				salaryList: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 1000,
					orderByColumn: "createTime",
				},
			};
		},
		onLoad() {
			if (isHaveBug("HR-APP-013")){
				this.isCanHd = false
			}
			if (isHaveBug("HR-APP-014")){
				this.isShowSigin = false
			}
			if (isHaveBug("HR-APP-015")){
				uni.setNavigationBarTitle({
					title:"合同信息"
				})
			}
			Promise.all([getDicts("contract_status")])
				.then(([contractStatus]) => {
					this.contractStatus = contractStatus.data;

					this.fetchSalaryData();
				})
				.catch((error) => {
					console.error("Error loading dictionary data:", error);
				});
		},
		methods: {
			goToDetails(id) {
				uni.navigateTo({
					url: `/pages/work/contractDetails/contractDetails?id=${id}`,
				});
			},
			isContractValid(endDate) {
				const currentDate = new Date();
				const contractEndDate = new Date(endDate);
				return contractEndDate >= currentDate;
			},
			fetchSalaryData() {
				contractList(this.queryParams)
					.then((response) => {
						this.salaryList = response.rows || [];

						this.salaryList.forEach((i) => {
							let status = this.contractStatus.find(
								(item) => item.dictValue == i.status
							);
							i.status = status.dictLabel;
						});
						console.log(response.rows);
					})
					.catch((error) => {
						console.error("Error fetching salary data:", error);
					});
			},
			showSignDate(value){
				if (this.isShowSigin){
					return value
				}else{
					return " "
				}
			},
			formatCurrency(value) {
				return Number(value)
					.toLocaleString("zh-CN", {
						style: "currency",
						currency: "CNY",
						minimumFractionDigits: 2,
					})
					.replace("CN¥", "");
			},
		},
	};
</script>

<style scoped>
	.no-salary {
		text-align: center;
		font-size: 16px;
		color: #888;
		padding: 20px;
	}

	.salary-item {
		padding: 15px;
		border-radius: 8px;
		background-color: white;
		margin: 10px 10px 10px;
		/* box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.05); */
	}

	.date-info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10px;
	}

	.label {
		font-weight: bold;
	}

	.valid {
		color: #28a745;
	}

	.expired {
		color: #dc3545;
	}

	.salary-info {
		display: flex;
		flex-direction: column;
	}

	.salary-detail {
		display: flex;
		justify-content: space-between;
		margin-bottom: 5px;
	}

	.title {
		font-weight: bold;
		color: #333;
	}

	.value {
		color: #555;
	}

	.divider {
		height: 1px;
		background-color: #e0e0e0;
		margin-top: 10px;
	}
</style>